package com.stormrage.edu.ctrl.exam;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.stormrage.edu.serv.adm.AdmAcctServ;
import com.stormrage.edu.serv.edu.EduCourseServ;
import com.stormrage.edu.serv.edu.EduModuleServ;
import com.stormrage.edu.serv.exam.ExmExamServ;
import com.stormrage.edu.serv.exam.ExmRelateCountServ;
import com.stormrage.edu.serv.exam.ExmScorePercentServ;
import com.stormrage.edu.serv.exam.ExmTermScoreServ;
import com.stormrage.edu.serv.learn.LeaTermServ;
import com.stormrage.entity.adm.AdmRes;
import com.stormrage.entity.exam.ExmRelateCount;
import com.stormrage.entity.exam.ExmScorePercent;
import com.stormrage.entity.exam.ExmTermScore;
import com.stormrage.entity.exam.rep.ExmCountRep;
import com.stormrage.entity.exam.rep.ExmExamCountRep;
import com.stormrage.entity.exam.req.ExmExamReq;
import com.stormrage.entity.exam.req.ExmRelateCountReq;
import com.stormrage.entity.learn.rep.LeaTermRep;
import com.stormrage.entity.learn.req.ExmTermNessReq;
import com.stormrage.entity.rep.ErrorRep;
import com.stormrage.entity.shiro.ShiroAcct;
import com.stormrage.shiro.bind.CurrentUser;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: ExmTermCtrl.java
 * @Package com.stormrage.edu.ctrl.exam
 * @Description: 学习考试 Controller
 * @author www.stormragetech.com
 * @date 2016年5月13日
 * @version V1.0
 */
@Controller
@RequestMapping("/exam/term")
public class ExmTermCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(ExmTermCtrl.class);

	@Value("#{constants['html.title']}")
	private String htmlTitle;

	@Autowired
	private AdmAcctServ admAcctServ;

	@Autowired
	private EduCourseServ eduCourseServ;

	@Autowired
	private EduModuleServ eduModuleServ;

	@Autowired
	private ExmExamServ exmExamServ;

	@Autowired
	private ExmRelateCountServ exmRelateCountServ;

	@Autowired
	private ExmScorePercentServ exmScorePercentServ;

	@Autowired
	private ExmTermScoreServ exmTermScoreServ;

	@Autowired
	private LeaTermServ leaTermServ;

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/")
	public String index(@CurrentUser ShiroAcct curr, Model model) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 index 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------
			List<LeaTermRep> array = leaTermServ.queryRepListForExam();

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 index 】-【 GET 】-【 COMPLETED 】");
		return "exam/term/index";
	}

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/{id}/perc", method = RequestMethod.GET)
	public String percent(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 percent 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------
			ExmScorePercent entity = exmScorePercentServ.queryByTermId(id);
			if (null != entity)
				model.addAttribute("data", entity);

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("id", id);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 percent 】-【 GET 】-【 COMPLETED 】");
		return "exam/term/perc";
	}

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/{id}/perc", method = RequestMethod.POST)
	public String percent(@CurrentUser ShiroAcct curr, ExmScorePercent entity, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 percent 】-【 POST 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ Ctrl操作 ------------------
			exmScorePercentServ.update(entity);

			errMsg = "提示信息：设置学期分数百分比成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = e.getMessage();
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/exam/term/{id}/perc";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 percent 】-【 POST 】-【 COMPLETED 】");
		return "redirect:/exam/term/";
	}

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/{id}/score", method = RequestMethod.GET)
	public String score(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 score 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------
			ExmTermScore entity = exmTermScoreServ.queryByTermId(id);
			if (null != entity)
				model.addAttribute("data", entity);

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("id", id);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 score 】-【 GET 】-【 COMPLETED 】");
		return "exam/term/score";
	}

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/{id}/score", method = RequestMethod.POST)
	public String score(@CurrentUser ShiroAcct curr, ExmTermScore entity, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 score 】-【 POST 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ Ctrl操作 ------------------
			exmTermScoreServ.update(entity);

			errMsg = "提示信息：设置学期题目分数成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = e.getMessage();
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/exam/term/{id}/score";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 score 】-【 POST 】-【 COMPLETED 】");
		return "redirect:/exam/term/";
	}

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/{id}/course", method = RequestMethod.GET)
	public String course(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 course 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------

			ExmTermNessReq req = ExmTermNessReq.instance(id, 0);
			ExmCountRep entity = eduCourseServ.queryRepListByTermNess(req);

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("id", id);
			model.addAttribute("data", entity);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 course 】-【 GET 】-【 COMPLETED 】");
		return "exam/term/course";
	}

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/{aid}/{id}/course", method = RequestMethod.GET)
	public String courseSetup(@CurrentUser ShiroAcct curr, @PathVariable("aid") Integer aid,
			@PathVariable("id") Integer id, Model model) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 courseSetup 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------

			ExmRelateCountReq req = ExmRelateCountReq.instance(aid, id, 0);
			ExmRelateCount entity = exmRelateCountServ.queryByTermRelateType(req);

			ExmExamReq examReq = new ExmExamReq(0, id);
			ExmExamCountRep rep = exmExamServ.queryRepByRelate(examReq);

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("id", id);
			model.addAttribute("data", entity);
			model.addAttribute("count", rep);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 courseSetup 】-【 GET 】-【 COMPLETED 】");
		return "exam/term/setup";
	}

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/{aid}/{id}/course", method = RequestMethod.POST)
	public String courseSetup(@CurrentUser ShiroAcct curr, ExmRelateCount req, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 courseSetup 】-【 POST 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ Ctrl操作 ------------------

			exmRelateCountServ.update(req);

			errMsg = "提示信息：设置题目数量成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = e.getMessage();
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/exam/term/{aid}/{id}/course";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 courseSetup 】-【 GET 】-【 COMPLETED 】");
		return "redirect:/exam/term/{aid}/course";
	}

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/{id}/mod", method = RequestMethod.GET)
	public String module(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 module 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------

			ExmTermNessReq req = ExmTermNessReq.instance(id, 0);
			ExmCountRep entity = eduModuleServ.queryRepListByTermNess(req);

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("id", id);
			model.addAttribute("data", entity);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 module 】-【 GET 】-【 COMPLETED 】");
		return "exam/term/mod";
	}

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/{aid}/{id}/mod", method = RequestMethod.GET)
	public String moduleSetup(@CurrentUser ShiroAcct curr, @PathVariable("aid") Integer aid,
			@PathVariable("id") Integer id, Model model) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 moduleSetup 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------

			ExmRelateCountReq req = ExmRelateCountReq.instance(aid, id, 1);
			ExmRelateCount entity = exmRelateCountServ.queryByTermRelateType(req);

			ExmExamReq examReq = new ExmExamReq(1, id);
			ExmExamCountRep rep = exmExamServ.queryRepByRelate(examReq);

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("id", id);
			model.addAttribute("data", entity);
			model.addAttribute("count", rep);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 moduleSetup 】-【 GET 】-【 COMPLETED 】");
		return "exam/term/setup";
	}

	@RequiresPermissions("exam:edit")
	@RequestMapping(value = "/{aid}/{id}/mod", method = RequestMethod.POST)
	public String moduleSetup(@CurrentUser ShiroAcct curr, ExmRelateCount req, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 moduleSetup 】-【 POST 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ Ctrl操作 ------------------

			exmRelateCountServ.update(req);

			errMsg = "提示信息：设置题目数量成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = e.getMessage();
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/exam/term/{aid}/{id}/mod";
		}

		logger.debug(" 【 Controller 】-【 ExmMajorCtrl 】-【 moduleSetup 】-【 GET 】-【 COMPLETED 】");
		return "redirect:/exam/term/{aid}/mod";
	}

}
